unit Thrad_SetQBDGL;

interface

uses
  Classes,SysUtils,Class_KzThrad,UniConnct,Class_QBD_FL,Class_ZBD_FL,Uni,
  Class_QBD_GL;

type
  TThradSetQBDGL = class(TKzThrad)
  public
    FListQBDFL:TStringList;
  protected
    procedure Execute; override;
  end;
  
const
  CONST_MARK_ZBGL='ZBGL';
  
implementation
uses
  ActiveX,Class_KzUtils;

procedure TThradSetQBDGL.Execute;
var
  I:Integer;
  SQLA:string;
  QBDFL:TQBDFL;
  QBDGL:TQBDGL;
  ZBDFL:TZBDFL;
  ListA:TStringList;
  UniConnct:TUniConnection;
begin
  CoInitialize(nil);

  try
    ListA:=TStringList.Create;
    QBDGL:=TQBDGL.Create;
    
    UniConnct:=UniConnctEx.GetConnection(CONST_MARK_ZBGL);
    try
      UniConnct.StartTransaction;
      //-<
      GetMaxProgress(FListQBDFL.Count);
      
      for I:=0 to FListQBDFL.Count-1 do
      begin
        QBDFL:=nil;
        QBDFL:=TQBDFL(FListQBDFL.Objects[I]);
        if QBDFL=nil then Continue;

        SQLA:=QBDFL.ExpSQL_IN_ZBDFL;
        TKzUtils.JustCleanList(ListA);
        TZBDFL.ListDB(SQLA,UniConnct,ListA);

        if ListA.Count=1 then
        begin
          ZBDFL:=TZBDFL(ListA.Objects[0]);

          QBDGL.JFBKDID   :=QBDFL.JFBKDID;
          QBDGL.JFBKDFLID :=QBDFL.JFBKDFLID;
          QBDGL.JFBKDDWBML:=QBDFL.JFBKDDWBML;
          QBDGL.JFBKDYBTAG:=QBDFL.JFBKDYBTAG;
          QBDGL.ZBDFLBH   :=ZBDFL.ZBDFLBH;
          QBDGL.ZBDBH     :=ZBDFL.ZBDBH;

          QBDGL.SaveItDB(UniConnct);

          //GetMsgProgress('SETQBDGL',[I+1,ListA.Count,ZBDFL.ZBDBH,ZBDFL.ZBDFLBH]);
          GetMsgProgress('SETQBDGL',[QBDFL.RowIndex,ListA.Count,ZBDFL.ZBDBH,ZBDFL.ZBDFLBH]);          
        end else
        begin
          //GetMsgProgress('SETQBDGL',[I+1,ListA.Count,'',0]);
          GetMsgProgress('SETQBDGL',[QBDFL.RowIndex,ListA.Count,'',0]);
        end;

        GetOneProgress(I+1);
      end;

      GetMaxProgress(0);
      GetOneProgress(0);
      //->
      UniConnct.Commit;
    except
      on E:Exception do
      begin
        UniConnct.Rollback;
        raise Exception.Create(E.Message);
      end;
    end;
  finally
    FreeAndNil(UniConnct);
    FreeAndNil(QBDGL);
    TKzUtils.TryFreeAndNil(ListA);
  end;

  CoUninitialize;      
end;

end.
